package cn.edu.scau.cmi.chenruilin.composite.consistent;

import java.util.ArrayList;
import java.util.Scanner;
import cn.edu.scau.cmi.chenruilin.composite.consistent.ConsistentComponent;
import cn.edu.scau.cmi.chenruilin.composite.consistent.ConsistentComposite;
import cn.edu.scau.cmi.chenruilin.composite.consistent.ConsistentLeaf;

public class ConsistentMenu {
	private static ConsistentComponent c_sc = new ConsistentComposite();
	private static ConsistentComponent p_sc = new ConsistentComposite();
	private static ConsistentComponent s_sc = new ConsistentLeaf();
	private static int space = 1;
	static String s = null;
	
	public static void print(ConsistentComposite organization, int space) {
		
		ArrayList<ConsistentComponent> chilren = organization.getChildren();
		for(int i = 0; i < chilren.size(); i++) {
			for(int j = 0; j < space; j++) {
				//System.out.print("        ");
				s += "        ";
			}
			ConsistentComponent sc = chilren.get(i);
			if(sc instanceof ConsistentLeaf) {
				ConsistentLeaf sc2 = (ConsistentLeaf) sc;
				s += "个人："+sc2.getName()+"\n";
				//System.out.println("个人："+sc2.getName());
			}
			else if(sc instanceof ConsistentComposite) {
				ConsistentComposite sc1 = (ConsistentComposite) sc;
				s += "团队："+sc1.getName()+"\n";
				//System.out.println("团队："+sc1.getName());
				if(sc1.getChildren()!=null) {
					print(sc1, space+1);
				}
			}
		}
	}
	
	public static void check(ConsistentComposite organization, String name) {
		ArrayList<ConsistentComponent> chilren = organization.getChildren();
		for(int i = 0; i < chilren.size(); i++) {
			ConsistentComponent sc = chilren.get(i);
			if(sc instanceof ConsistentComposite) {
				ConsistentComposite sc1 = (ConsistentComposite) sc;
				if(sc1.getName().compareTo(name)==0) {
					c_sc = sc1;
					return;
				}
				if(sc1.getChildren()!=null) {
					check(sc1, name);
				}
			}
			if(sc instanceof ConsistentLeaf) {
				ConsistentLeaf sf = (ConsistentLeaf) sc;
				if(sf.getName().compareTo(name)==0) {
					s_sc = sf;
					return;
				}
			}
		}
	}
	
	public static void menu() {
		Scanner in = new Scanner(System.in);
		int flag = 0, flag1 = 0;;
		ConsistentComposite organization = new ConsistentComposite();
		System.out.println("请输入创建组织名称！");
		String name = in.next();
		organization.setName(name);
		while(true) {
			c_sc = null;
			p_sc = organization;
			s_sc = null;
			
			System.out.println(s);
			s = null;
			System.out.println("请选择功能\n1.添加团队	2.添加个人	3.删除");
			int input = in.nextInt();
			switch(input) {
			case(1):{
				ArrayList<ConsistentComponent> chilren = organization.getChildren();
				if(chilren.size() == 0) {
					System.out.println("请输入添加团队的名称");
					String t_name = in.next();
					ConsistentComposite team = new ConsistentComposite();
					team.setName(t_name);
					organization.addChild(team);
					s = "组织："+organization.getName()+"\n";
					
					//System.out.println("组织："+organization.getName());
					
					print(organization, space);
					break;
				}
				System.out.println("请输入添加团队的父亲节点");
				String p_name = in.next();
				if(p_name.compareTo(organization.getName()) == 0) {
					System.out.println("请输入添加团队的名称");
					String t_name = in.next();
					ConsistentComposite team = new ConsistentComposite();
					team.setName(t_name);
					organization.addChild(team);
					s = "组织："+organization.getName()+"\n";
					//System.out.println("组织："+organization.getName());
					print(organization, space);
					break;
				}
				check(organization, p_name);
				if(c_sc == null) {
					System.out.println("请输入正确的团队名称作为父亲节点！");
				}
				else {
					System.out.println("请输入添加团队的名称");
					String t_name = in.next();
					ConsistentComposite team = new ConsistentComposite();
					team.setName(t_name);
					c_sc.addChild(team);
					s = "组织："+organization.getName()+"\n";
					//System.out.println("组织："+organization.getName());
					print(organization, space);
				}
				break;
			}
			
			
			case(2):{
				ArrayList<ConsistentComponent> chilren = organization.getChildren();
				if(chilren.size() == 0) {
					System.out.println("请输入添加个人的名称");
					String person_name = in.next();
					ConsistentLeaf person = new ConsistentLeaf();
					person.setName(person_name);
					organization.addChild(person);
					s = "组织："+organization.getName()+"\n";
					//System.out.println("组织："+organization.getName());
					print(organization, space);
					break;
				}
				System.out.println("请输入添加个人的父亲节点");
				String p_name = in.next();
				if(p_name.compareTo(organization.getName()) == 0) {
					//System.out.println("112");
					System.out.println("请输入添加个人的名称");
					String person_name = in.next();
					ConsistentLeaf person = new ConsistentLeaf();
					person.setName(person_name);
					organization.addChild(person);
					//System.out.println("111");
					s = "组织："+organization.getName()+"\n";
					//System.out.println("组织："+organization.getName());
					print(organization, space);
					break;
				}
				check(organization, p_name);
				if(c_sc == null) {
					System.out.println("请输入正确的团队名称作为父亲节点！");
				}
				else {
					System.out.println("请输入添加个人的名称");
					String person_name = in.next();
					ConsistentLeaf person = new ConsistentLeaf();
					person.setName(person_name);
					c_sc.addChild(person);
					s = "组织："+organization.getName()+"\n";
					//System.out.println("组织："+organization.getName());
					print(organization, space);
				}
				break;
				
			}
			case(3):{
				ArrayList<ConsistentComponent> chilren = organization.getChildren();
				System.out.println("请输入删除的团队或个人名称");
				String d_name = in.next();

				if(chilren.size() == 0 && d_name.compareTo(organization.getName())==0) {
					System.out.println("要删除组织请关闭重新运行程序");
					break;
				}
				check(organization, d_name);		
				if(c_sc == null && s_sc == null) {
					System.out.println("该名称不存在！");
				}
				else if(c_sc!=null && s_sc==null){
					p_sc = c_sc.getParent();
					p_sc.deleteChild(c_sc);		
					s = "组织："+organization.getName()+"\n";
					//System.out.println("组织："+organization.getName());
					print(organization, space);
				}
				else if(s_sc!=null && c_sc==null) {
					System.out.println("删除个人");
					p_sc = s_sc.getParent();
					p_sc.deleteChild(s_sc);		
					s = "组织："+organization.getName()+"\n";
					//System.out.println("组织："+organization.getName());
					print(organization, space);
				}
				
				break;
			}
			default:  {
				System.out.println("您的输入有误，请重新输入!");
				continue;
			}
			
			}
		}
		
	}
}
